﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using Microsoft.Win32;
using PdfLibrary;
using Castle.DynamicProxy;
using System.Reflection;
using TtfLibrary;

namespace WpfViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region etc
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
            if(item == cbiFitToHeight)
            {
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
                scrollViewer.HorizontalAlignment = HorizontalAlignment.Stretch;
                scrollViewer.VerticalAlignment = VerticalAlignment.Stretch;

            } 
            else if (item == cbiFitToWidth)
            {
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.HorizontalAlignment = HorizontalAlignment.Stretch;
                scrollViewer.VerticalAlignment = VerticalAlignment.Stretch;
            }
            else if(item == cbiOriginalSize)
            {
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.HorizontalAlignment = HorizontalAlignment.Center;
                scrollViewer.VerticalAlignment = VerticalAlignment.Center;
                //scrollViewer.Height = double.NaN;
            }
        }
        #endregion

        private PdfFile pdf;

        private void PdfTest()
        {
            OpenFile("android.pdf");
        }

        private void OpenFile(string fn)
        {
            pdf = new PdfFile(fn);
            OpenPage(1);            
        }

        private void OpenPage(int pageNumber)
        {
            /*var encString = ((pdf.Objects[2].content as PdfDictionary)["Producer"] as PdfString).Value;
            var encDict = pdf.Objects[25].content as PdfDictionary;*/

            var page = pdf.Trailer.Root.Pages.Kids[pageNumber - 1] as IPdfPageObject;
            //var zip = (page.Contents as PdfStream).streamData;
            //new GZipStream(new MemoryStream(zip), CompressionMode.Decompress).CopyTo(new MemoryStream());
            /*File.WriteAllBytes("page.gz", zip);
            var pageContent = (page.Contents as PdfStream).DecodedData;*/

            var pageContent = (page.Contents as PdfStream).DecodedData;

            //File.WriteAllBytes("pageContent.txt", pageContent);

            var reader = new PdfGraphicsReader(pageContent);
            var commands = new PdfGraphicsCommands(reader);

            drawCanvas.Children.Clear();

            var environment = new PdfGraphicsWpfEnvironment
            {
                Canvas = drawCanvas,
                PageWidth = page.MediaBox.Width,
                PageHeight = page.MediaBox.Height,
                Resources = page.Resources
            };

            drawCanvas.Width = page.MediaBox.Width;
            drawCanvas.Height = page.MediaBox.Height;

            //File.WriteAllText("Unknown commands.txt", String.Join(", ", commands.Commands.OfType<PdfCommandUnknown>().Select(c => c.Code).Distinct()));
            //var x = new GlyphTypeface(new Uri(@"c:\dropbox\prg_shared\PdfEditor001\WpfViewer\bin\Debug\font.ttf"));
            //File.WriteAllBytes("font.ttf", (pdf2.Objects[28].content as PdfStream).DecodedData);)

            foreach (var cmd in commands.Commands)
                cmd.Execute(environment);
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            PdfTest();
        }

        private void txtPageNum_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
                OpenPage(int.Parse(txtPageNum.Text));
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog() { DefaultExt = "pdf" };
            if (dlg.ShowDialog() == true)
                OpenFile(dlg.FileName);
        }
    }
}
